package com.sctvcloud.qingchuan.ui.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.ArrayMap;

import com.jiongbull.jlog.JLog;
import com.mob.tools.utils.DeviceHelper;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.base.BaseActivity;
import com.sctvcloud.qingchuan.beans.NewsInfoAdvertBean;
import com.sctvcloud.qingchuan.beans.NewsItem;
import com.sctvcloud.qingchuan.beans.SingleResult;
import com.sctvcloud.qingchuan.http.AbsNetCallBack;
import com.sctvcloud.qingchuan.http.NetUtils;
import com.sctvcloud.qingchuan.ui.activities.AdvertPicInfoActivity;
import com.sctvcloud.qingchuan.ui.activities.CountyNewsDetailActivity;
import com.sctvcloud.qingchuan.ui.activities.H5NormalActivity;
import com.sctvcloud.qingchuan.ui.activities.LivingActivity;
import com.sctvcloud.qingchuan.ui.activities.NewesDetailActivity;
import com.sctvcloud.qingchuan.ui.activities.PoliticsDetailActivity;
import com.sctvcloud.qingchuan.ui.activities.TopicDetailActivity;
import com.sctvcloud.qingchuan.ui.activities.VideoPlayActivity;
import com.sctvcloud.qingchuan.utils.UserManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by liuyu on 2017/8/21.
 * <p>
 * 进入新闻详情、H5等页面的工具管理类
 */

public class SkipUtil {

    public static final int REQUEST_CODE_PLAY_BASE = 121;
    public static final String EX_POS = VideoPlayActivity.EXTRA_POS;
    public static final String EX_POS_ITEM = VideoPlayActivity.EXTRA_POS_ITEM;

    /**
     * 跳转到详情页（新闻、专题、直播、H5等）
     *
     * @param context
     * @param item
     * @param tag
     * @return
     */
    public static boolean skipWithNewsItem(@NonNull Context context, NewsItem item, HashMap<String, String> tag) {
        if (item == null || !item.isNewsTypeValued()) {
            return false;
        }
        Intent intent = null;
        switch (item.getNewsType()) {
            case Constances.NEWS_TYPE_GRAPHIC:
            case Constances.NEWS_TYPE_AUDIO:
            case Constances.NEWS_TYPE_VIDEO:
                //新闻详情
                intent = new Intent(context, NewesDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_TOPIC:
                //专题详情
                intent = new Intent(context, TopicDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_LIVE:
                //直播详情
                intent = new Intent(context, LivingActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                intent.putExtra(BaseActivity.EX_DATA, item.getNewsImage());
                break;
            case Constances.NEWS_TYPE_H5:
                //H5
                intent = new Intent(context, H5NormalActivity.class);
                intent.putExtra(H5NormalActivity.H5_NEWS_ID, item.getNewsId());
                intent.putExtra(H5NormalActivity.EX_HTML, item.getNewsUrl());
                intent.putExtra(H5NormalActivity.EX_TITLE, item.getNewsTitle());
                break;
            case Constances.NEWS_TYPE_COUNTY:
                //区县新闻详情
                intent = new Intent(context, CountyNewsDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_SMALL_AD:
            case Constances.NEWS_TYPE_BIGGER_AD:
                //小图广告、大图广告（24小时新闻中）
                if (item.getAdvertType().equalsIgnoreCase("0") && !TextUtils.isEmpty(item.getAdvertContentImage())) {
                    //跳转图片预览
                    intent = new Intent(context, AdvertPicInfoActivity.class);
                    List<String> pics = new ArrayList<>();
                    pics.add(item.getAdvertContentImage());
                    intent.putExtra(AdvertPicInfoActivity.PIC_LIST, (Serializable) pics);
                    List<NewsInfoAdvertBean> picInfo = new ArrayList<>();
                    NewsInfoAdvertBean infoBean = new NewsInfoAdvertBean();
                    infoBean.setId(item.getNewsId());
                    infoBean.setContentImage(item.getAdvertContentImage());
                    infoBean.setH5(item.getAdvertH5());
                    infoBean.setName(item.getNewsTitle());
                    picInfo.add(infoBean);
                    intent.putExtra(AdvertPicInfoActivity.PIC_INFO_LIST, (Serializable) picInfo);
                } else if (item.getAdvertType().equalsIgnoreCase("2") && !TextUtils.isEmpty(item.getAdvertH5())) {
                    //跳转h5外链
                    intent = new Intent(context, H5NormalActivity.class);
                    intent.putExtra(H5NormalActivity.EX_HTML, item.getAdvertH5());
                    intent.putExtra(H5NormalActivity.H5_NEWS_ID, item.getNewsId());
                    intent.putExtra(H5NormalActivity.IS_ADVERT_H5, true);
                    intent.putExtra(H5NormalActivity.IS_NEED_SHARE, true);
                    intent.putExtra(H5NormalActivity.IS_NEED_TOKEN, false);
                }
                break;

        }
        if (intent != null) {
            intent.putExtra("TAG", tag);
            if (item.isLuzhouShow()) {
                intent.putExtra(Constances.IS_CITY_STATE, true);
            }
            context.startActivity(intent);
            return true;
        }
        return false;
    }

    /**
     * 跳转到详情页（新闻、专题、直播、H5等）
     *
     * @param context
     * @param item
     * @param tag
     * @return
     */
    public static Intent getIntentWithNewsItem(@NonNull Context context, NewsItem item, HashMap<String, String> tag) {
        if (item == null || !item.isNewsTypeValued()) {
            return null;
        }
        Intent intent = null;
        switch (item.getNewsType()) {
            case Constances.NEWS_TYPE_GRAPHIC:
            case Constances.NEWS_TYPE_AUDIO:
            case Constances.NEWS_TYPE_VIDEO:
                //新闻详情
                intent = new Intent(context, NewesDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_TOPIC:
                //专题详情
                intent = new Intent(context, TopicDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_LIVE:
                //直播详情
                intent = new Intent(context, LivingActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                intent.putExtra(BaseActivity.EX_DATA, item.getNewsImage());
                break;
            case Constances.NEWS_TYPE_H5:
                //H5
                intent = new Intent(context, H5NormalActivity.class);
                intent.putExtra(H5NormalActivity.H5_NEWS_ID, item.getNewsId());
                intent.putExtra(H5NormalActivity.EX_HTML, item.getNewsUrl());
                intent.putExtra(H5NormalActivity.EX_TITLE, item.getNewsTitle());
                break;
            case Constances.NEWS_TYPE_COUNTY:
                //区县新闻详情
                intent = new Intent(context, CountyNewsDetailActivity.class);
                intent.putExtra(H5NormalActivity.EX_HTML, item.getNewsUrl());
                break;
        }
        if (intent != null) {
            intent.putExtra("TAG", tag);
//			context.startActivity(intent);
            return intent;
        }
        return null;
    }

    /**
     * 跳转到视频播放页
     *
     * @param ac
     * @param url
     * @param playedPos
     * @param itemPos
     */
    public static void skipToVideoPlay(Activity ac, String url, int playedPos, int itemPos) {
//        ac.startActivityForResult();
        if (ac == null || TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent(ac, VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        ac.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }

    /**
     * 跳转到视频播放页
     *
     * @param fg
     * @param url
     * @param playedPos
     * @param itemPos
     */
    public static void skipToVideoPlay(Fragment fg, String url, int playedPos, int itemPos) {
//        ac.startActivityForResult();
        if (fg == null || TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent(fg.getContext(), VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        fg.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }


    /**
     * 跳转到视频播放页
     *
     * @param fg
     * @param url
     * @param playedPos
     * @param itemPos
     * @param coverImgUrl
     */
    public static void skipToVideoPlayWithCover(Fragment fg, String url, int playedPos, int itemPos, String coverImgUrl) {
//        ac.startActivityForResult();
        if (fg == null || TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent(fg.getContext(), VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        intent.putExtra(VideoPlayActivity.EXTRA_COVER, coverImgUrl);
        fg.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }


    /**
     * 跳转到视频播放页
     *
     * @param fg
     * @param url
     * @param playedPos
     * @param itemPos
     * @param coverImgUrl
     */
    public static void skipToVideoPlayWithCover(Activity fg, String url, int playedPos, int itemPos, String coverImgUrl) {
//        ac.startActivityForResult();
        if (fg == null || TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent(fg, VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        intent.putExtra(VideoPlayActivity.EXTRA_COVER, coverImgUrl);
        fg.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }

    /**
     * 跳转到问政详情
     *
     * @param context
     */
    public static void skipToPoliticsInfo(@NonNull Context context,  long  id ) {
//        ac.startActivityForResult();
        if (context == null ||id == 0) {
            return;
        }
        Intent intent = new Intent(context, PoliticsDetailActivity.class);
        intent.putExtra(PoliticsDetailActivity.POLITICS_ID,id);
        context.startActivity(intent);
    }

    /**
     * 上传统计 日志
     *
     * @param ctx
     * @param owner
     * @param //siteNumber 站点掌上青川为1
     * @param resurceType  来源，1直播观看、2新闻阅读，12投稿、14分享等
     * @param typeName     1直播，2主播圈，3投稿，4视频新闻，5音频新闻，6图文新闻
     * @param decription   描述。例如登陆成功
     * @param newsId       新闻id没有不传
     * @param nodeId       栏目ID，例如，专题，高层 等，传id
     */
    public static void skipToAddUserOperate(Context ctx, final String owner, final int resurceType, final int typeName, final String decription, final String newsId, final String nodeId) {
        String userPhone = "";
        if (UserManager.isLoginS()) {
            userPhone = UserManager.getInstance().getUser().getPhoneNumber();
        }
        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("siteNumber", Constances.SITE_ID);
        p.put("resurceType", resurceType);
        if (typeName > 0) {
            p.put("typeName", typeName);
        }
        p.put("decription", decription);
        if (!TextUtils.isEmpty(userPhone)) {
            p.put("cellPhone", userPhone);
        }
        if (!TextUtils.isEmpty(newsId)) {
            p.put("newsId", newsId);
        }
        if (!TextUtils.isEmpty(nodeId)) {
            p.put("nodeId", nodeId);
        }
        p.put("device", DeviceHelper.getInstance(ctx).getDeviceId());
        JLog.e("skipToAddUserOperate--", owner + "  resurceType=" + resurceType + " typeName=" + typeName + "  cellPhone=" + userPhone + "  decription=" + decription + "  newsId=" + newsId + "  nodeId=" + nodeId + " Device=" + DeviceHelper.getInstance(ctx).getDeviceId());
        NetUtils.getNetAdapter().addUserOperate(owner, p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult info) {
                JLog.e("skipToAddUserOperate 成功--", owner + " -- " + resurceType + " " + typeName + " " + newsId + " " + nodeId + "  decription=" + decription);
            }

            @Override
            public void onError(Throwable ex, String errorMsg, int netCode) {
                super.onError(ex, errorMsg, netCode);
                JLog.e("skipToAddUserOperate 失败--", owner + " -- " + resurceType + " " + typeName + " " + newsId + " " + nodeId);
            }

            @Override
            public void onEnd() {
                super.onEnd();
            }
        });
    }

    /**
     * 上传统计 日志
     *
     * @param ctx
     * @param owner
     * @param //siteNumber 站点掌上青川为1
     * @param resurceType  来源，1直播观看、2新闻阅读，12投稿、14分享等
     * @param typeName     1直播，2主播圈，3投稿，4视频新闻，5音频新闻，6图文新闻
     * @param decription   描述。例如登陆成功
     * @param newsId       新闻id没有不传
     * @param nodeId       栏目ID，例如，专题，高层 等，传id
     * @param phoneNumber  用户手机号
     */
    public static void skipToAddUserOperate(Context ctx, final String owner, final int resurceType, final int typeName, final String decription, final String newsId, final String nodeId, final String phoneNumber) {

        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("siteNumber", Constances.SITE_ID);
        p.put("resurceType", resurceType);
        if (typeName > 0) {
            p.put("typeName", typeName);
        }
        p.put("decription", decription);
        if (!TextUtils.isEmpty(phoneNumber)) {
            p.put("cellPhone", phoneNumber);
        }
        if (!TextUtils.isEmpty(newsId)) {
            p.put("newsId", newsId);
        }
        if (!TextUtils.isEmpty(nodeId)) {
            p.put("nodeId", nodeId);
        }
        p.put("device", DeviceHelper.getInstance(ctx).getDeviceId());
        JLog.e("skipToAddUserOperate--", owner + "  resurceType=" + resurceType + " typeName=" + typeName + "  cellPhone=" + phoneNumber + "  decription=" + decription + "  newsId=" + newsId + "  nodeId=" + nodeId + " Device=" + DeviceHelper.getInstance(ctx).getDeviceId());
        NetUtils.getNetAdapter().addUserOperate(owner, p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult info) {
                JLog.e("skipToAddUserOperate 成功--", owner + " -- " + resurceType + " " + typeName + " " + newsId + " " + nodeId + "  decription=" + decription);
            }

            @Override
            public void onError(Throwable ex, String errorMsg, int netCode) {
                super.onError(ex, errorMsg, netCode);
                JLog.e("skipToAddUserOperate 失败--", owner + " -- " + resurceType + " " + typeName + " " + newsId + " " + nodeId);
            }

            @Override
            public void onEnd() {
                super.onEnd();
            }
        });
    }

    /**
     * 发送在线统计
     *
     * @param ctx
     * @param owner
     * @param id       直播、频道、广播ID
     * @param liveType 1 直播 ，2 频道 3 广播
     */
    public static void skipToAddOnline(Context ctx, String owner, final String id, final int liveType) {
        String userPhone = "";
        if (UserManager.isLoginS()) {
            userPhone = UserManager.getInstance().getUser().getPhoneNumber();
        }
        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("liveId", id);
        p.put("userId", userPhone);
        p.put("type", liveType);
        p.put("deviceId", DeviceHelper.getInstance(ctx).getDeviceId());
        NetUtils.getNetAdapter().addUserOnline(owner, p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult info) {
                JLog.e("上传 在线统计日志 成功-- id = " + id + "  --type=" + liveType);
            }

            @Override
            public void onError(Throwable ex, String errorMsg, int netCode) {
                super.onError(ex, errorMsg, netCode);
                JLog.e("上传 在线统计日志 失败-- id = " + id + "  --type=" + liveType);
            }

            @Override
            public void onEnd() {
                super.onEnd();

            }
        });
    }

}
